package com.tingyuge.thinking.spring.configuration.bean.definition;

import com.tingyuge.thinking.spring.configuration.bean.factory.DefaultUserFactory;
import com.tingyuge.thinking.spring.configuration.bean.factory.UserFactory;
import org.springframework.beans.factory.config.SingletonBeanRegistry;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @author xiaodao
 * 注册实例
 * @version 1.0
 * @date 2021/6/27
 */
public class SingletonBeanRegistrationDemo {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();


        UserFactory defaultUserFactory = new DefaultUserFactory();

        SingletonBeanRegistry singletonBeanRegistry = applicationContext.getBeanFactory();
        singletonBeanRegistry.registerSingleton("userFactory",defaultUserFactory);
        applicationContext.refresh();

        //通过依赖查找的方式
        UserFactory userFactoryLookUp = applicationContext.getBean("userFactory", UserFactory.class);
        System.out.println("userFactory== userFactoryLookUp :"+ (userFactoryLookUp==defaultUserFactory));

    }
}
